<template>
    <div class="singer-detail">
        <music-list :songs="songs" :title="title" :bgImg="bgImg"></music-list>
    </div>
</template>
<script>
import {mapGetters} from 'vuex'
import {getSingerDetail} from 'api/singer'
import {ERR_OK} from 'api/config'
import {createSong} from 'common/js/songs'
import MusicList from 'components/music-list/music-list'

export default {
  name: 'SingerDetail',
  data () {
    return {
      songs: []
    }
  },
  computed: {
    title () {
      return this.getSinger().name
    },
    bgImg () {
      return this.getSinger().avatar
    }
  },
  methods: {
    ...mapGetters({
      getSinger: 'singer'
    }),
    _getSingerDetail () {
      if (!this.getSinger().id) {
        this.$router.push('/singer')
        return
      }
      getSingerDetail(this.getSinger().id)
        .then((res) => {
          if (res.code === ERR_OK) {
            this.songs = this._normalizeSongs(res.data.list)
            // console.log(this.songs)
          }
        })
    },
    _normalizeSongs (list) {
      let ret = []
      list.forEach((item) => {
        let {musicData} = item
        if (musicData.songid && musicData.albumid) {
          ret.push(createSong(musicData))
        }
      })
      return ret
    }
  },
  created () {
    this._getSingerDetail()
  },
  components: {
    MusicList
  }
}
</script>
<style lang="scss" scoped>
@import 'common/scss/variable.scss';
.singer-detail{
  background: $color-background;
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  z-index: 99;
  color: #fff;
}
</style>
